//
//  TaskDetailViewController.m
//  OAProject
//
//  Created by yhj on 2017/3/27.
//  Copyright © 2017年 cdnunion. All rights reserved.
//   任务详情主界面

#import "TaskDetailViewController.h"
#import "CommonTableViewCell.h"
#import "TaskProcessViewController.h"
#import "TaskLogViewController.h"
#import "FileViewController.h"
#import "UpdateTaskLogViewController.h"
#import "EditTaskViewController.h"
#import "AddTaskProcessViewController.h"
#import "LQPopUpView.h"
#import "EvaluateTaskViewController.h"
#import "TaskRateTableViewCell.h"
#import "SelectContactListViewController.h"
#import "SelectProgressViewController.h"
#import "TaskListViewController.h"
#import "TaskLogTableViewCell.h"
#import "TaskLogDetailViewController.h"



static NSString *TaskLogTableViewCellIdentifier=@"TaskLogTableViewCellIdentifier";
static NSString *TaskRateTableViewCellIdentifier=@"TaskRateTableViewCellIdentifier";
static NSString *CommonTableViewCellIdentifier=@"CommonTableViewCellIdentifier";
@interface TaskDetailViewController ()<UITableViewDelegate,UITableViewDataSource,SelectContactListViewControllerDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)UIView *taskInfoView;

@property(nonatomic,strong)NSDictionary *taskDetailDic;

@property(nonatomic,strong)NSString *leftBtnTitleStr;

@property(nonatomic,strong)NSString *rightBtnTitleStr;

@property(nonatomic,strong)NSString *midBtnTitleStr;

@property(nonatomic,strong)UIView *bottomView;

@property(nonatomic,strong)NSDictionary *rateDic;

@property(nonatomic,strong)NSMutableArray *userNameArr;

@property(nonatomic,strong)NSMutableArray *userIDArr;

// 是否存在底部视图
@property(nonatomic,assign)BOOL isBottomView;

@property(nonatomic,strong)NSMutableArray *taskLogArr;

// 当前页
@property (nonatomic,assign)int currentPage;

@end

@implementation TaskDetailViewController

// delegate
-(void)selectContactUserNameArr:(NSMutableArray *)userNameArr userIDArr:(NSMutableArray *)userIDArr
{
    self.userNameArr=userNameArr;
    self.userIDArr=userIDArr;
}

-(void)turnOverTask
{
    [AppUI initNavigationItem:self.navigationItem andWithText:@"转  接" andTextColor:WhiteColor andIsRight:YES withBlock:^{

        // 转接任务
        SelectContactListViewController *vc=[SelectContactListViewController new];
        vc.typeStr=@"turnOver";
        vc.delegate=self;
        vc.taskID=self.model.taskID;
        [self.navigationController pushViewController:vc animated:NO];
    }];
}

// 获取评价信息
-(void)GetTaskEvaluateInfoData
{
    WEAKSELF;
    [API GetTaskEvaluateInfoRefID:self.model.taskID ref:@"task" success:^(id res) {

        if ([res[@"rt"] isEqual:@(1)])
        {
            self.rateDic=res;
        }
        else
        {
          [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
        }

        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];

    } failure:^(NSInteger stateCode) {

         [weakSelf.tableView.mj_header endRefreshing];
        
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.taskLogArr=[NSMutableArray array];

    [self addHeader];

    [self addFooter];

}

-(void)addHeader
{
    WEAKSELF;
    self.tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage=1;

        [weakSelf.taskLogArr removeAllObjects];

        // 获取任务详情数据
        [weakSelf GetTaskDetailData];

        [weakSelf GetTaskLogData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

-(void)addFooter
{
    WEAKSELF;
    MJRefreshAutoNormalFooter *footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _currentPage += 1;

        // 获取任务详情数据
        [weakSelf GetTaskDetailData];

        // 获取任务日志列表数据
        [weakSelf GetTaskLogData];

    }];
    [footer setTitle:@"我是有底线的" forState:MJRefreshStateNoMoreData];
    footer.automaticallyHidden=YES;
    self.tableView.mj_footer=footer;
}


-(void)GetData
{
    self.tableView.mj_header=[MJRefreshNormalHeader
                              headerWithRefreshingBlock:^{

                                  // 获取任务详情数据
                                  [self GetTaskDetailData];

                                  // 获取日志数据
                                  [self GetTaskLogData];

                              }];
    [self.tableView.mj_header beginRefreshing];
}

-(void)GetTaskDetailData
{
    WEAKSELF;
   [API GetTaskDetailWithTaskID:self.model.taskID success:^(id res) {

        if ([res[@"rt"] isEqual:@(1)])
        {
            self.taskDetailDic=res;

            // 任务已完成获取评价信息
            if ([self.taskDetailDic[@"status"] isEqualToString:@"10"])
            {
                [self GetTaskEvaluateInfoData];
            }

              [self initData];
        }
       else
       {
          [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
       }

        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];

   } failure:^(NSInteger stateCode) {

        [self.tableView.mj_header endRefreshing];
   }];
}


-(void)GetTaskLogData
{
    WEAKSELF;
    [API GetTaskLogListWithTaskID:self.model.taskID stepID:@"" per_page:KCountNumber page_num:[NSString stringWithFormat:@"%d",self.currentPage] success:^(id res) {

        [weakSelf endRefresh:self.tableView array:self.taskLogArr];

        if ([res[@"rt"] isEqual:@(1)])
        {
            NSArray *array=res[@"rows"];
            for (NSDictionary *dic in array)
            {
                TaskLogListModel *model=[TaskLogListModel yy_modelWithDictionary:dic];
                [weakSelf.taskLogArr addObject:model];
            }

            if (self.currentPage<[res[@"page"][@"pages"] integerValue])
            {
                [weakSelf.tableView.mj_footer resetNoMoreData];
            }
            else
            {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        else
        {
            [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
        }
        [weakSelf.tableView reloadData];

    } failure:^(NSInteger stateCode) {
        
        [weakSelf requestFail:self.tableView array:self.taskLogArr];

    }];
}

#pragma mark 上下拉刷新
- (void)endRefresh:(UITableView *)tableView array:(NSMutableArray *)array
{
    if ([tableView.mj_header isRefreshing])
    {
        [array removeAllObjects];
        [tableView.mj_header endRefreshing];
    }
    if ([tableView.mj_footer isRefreshing])
    {
        [tableView.mj_footer endRefreshing];
    }
}

// 收到请求失败的通知
- (void)requestFail:(UITableView *)tableView array:(NSMutableArray *)array
{
    if ([tableView.mj_header isRefreshing])
    {
        [tableView.mj_header endRefreshing];
    }
    if ([tableView.mj_footer isRefreshing])
    {
        [tableView.mj_footer endRefreshing];
    }
    [array removeAllObjects];
    [tableView reloadData];
}



- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view addSubview:self.tableView];

}

-(void)initTableView
{
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,APPW,(_isBottomView==YES)?(APPH-kmargin*10):APPH) style:UITableViewStylePlain];
    _tableView.dataSource=self;
    _tableView.delegate=self;
    _tableView.backgroundColor=MAIN_BACK_COLOR;
    _tableView.showsVerticalScrollIndicator=NO;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    self.tableView.tableFooterView=[UIView new];
      [self.view addSubview:self.tableView];
    _tableView.tableHeaderView=self.taskInfoView;
    [self.tableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:CommonTableViewCellIdentifier];
    [self.tableView registerClass:[TaskRateTableViewCell class] forCellReuseIdentifier:TaskRateTableViewCellIdentifier];
    [self.tableView registerClass:[TaskLogTableViewCell class] forCellReuseIdentifier:TaskLogTableViewCellIdentifier];
}

-(void)initData
{
    self.title=[Util TaskStatusTitleByNumber:self.taskDetailDic[@"status"]];

    if ([self.taskDetailDic[@"opts"] count]>0)
    {
        __block BOOL isRefresh=NO;
        [self.taskDetailDic[@"opts"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            // 任务处理 拒绝与接受
            if ([obj isEqualToString:@"deal"])
            {
                self.leftBtnTitleStr=@"拒  绝";
                self.rightBtnTitleStr=@"接  受";

                // 转接任务
                [self turnOverTask];
            }
            // 任务状态更新
            else if ([obj isEqualToString:@"refresh"])
            {
                isRefresh=YES;
                *stop=YES;
            }
            // 任务验收
            else if ([obj isEqualToString:@"acceptance"])
            {
                self.leftBtnTitleStr=@"拒  绝";
                self.rightBtnTitleStr=@"通  过";
            }
            // 任务评价
            else  if ([obj isEqualToString:@"evaluate"])
            {
                self.midBtnTitleStr=@"评价";
            }
        }];
        
        if (isRefresh)
        {
            self.midBtnTitleStr=@"更  新";
        }

        [self initbottomView];

        [self.tableView reloadData];
    }
    else
    {
        _isBottomView=NO;
        self.bottomView.hidden=YES;
    }
}

-(void)initbottomView
{
    // 底部视图存在的情况
    [self.taskDetailDic[@"opts"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
     {
         if ([obj isEqualToString:@"deal"]||[obj isEqualToString:@"refresh"]||[obj isEqualToString:@"evaluate"]||[obj isEqualToString:@"acceptance"])
         {
             _bottomView=[[UIView alloc]initWithFrame:CGRectMake(0,APPH-kmargin*6,APPW,kmargin*7)];
             _bottomView.backgroundColor=WhiteColor;
             [self.view addSubview:self.bottomView];
             _isBottomView=YES;

             UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0,0,APPW,2)];
             lineView.backgroundColor=BGColor;
             [self.bottomView addSubview:lineView];
         }
     }];

     // 日志更新 与 评价
    [self.taskDetailDic[@"opts"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
     {
         if ([obj isEqualToString:@"refresh"])
         {
             UIButton *refreshBtn=[AppUI createButtonWithText:self.midBtnTitleStr andWithTextColor:WhiteColor andWithFontfloat:16 andWithBtnAction:^{
                 {
                     UpdateTaskLogViewController *vc=[UpdateTaskLogViewController new];
                     vc.taskModel=self.model;
                     vc.tempo=self.taskDetailDic[@"step_tempo"];
                     vc.isStop=[self.model.status isEqualToString:@"5"]?YES:NO;
                     [self.navigationController pushViewController:vc animated:NO];
                 }
             }];
             [self.bottomView addSubview:refreshBtn];
             refreshBtn.backgroundColor=MainColor;
             ViewBorderRadius(refreshBtn,kmargin/2,1,ClearColor);
             refreshBtn.frame=CGRectMake((APPW-(APPW-kmargin*7/2)/2)/2,kmargin,(APPW-kmargin*7/2)/2,kmargin*4);
         }
         // 任务评分
         else if ([obj isEqualToString:@"evaluate"])
         {
             UIButton *refreshBtn=[AppUI createButtonWithText:self.midBtnTitleStr andWithTextColor:WhiteColor andWithFontfloat:16 andWithBtnAction:^{

                 EvaluateTaskViewController *vc=[EvaluateTaskViewController new];
                 vc.model=self.model;
                 [self.navigationController pushViewController:vc animated:NO];
             }];
             [self.bottomView addSubview:refreshBtn];
             refreshBtn.backgroundColor=MainColor;
             ViewBorderRadius(refreshBtn,kmargin/2,1,ClearColor);
             refreshBtn.frame=CGRectMake((APPW-(APPW-kmargin*7/2)/2)/2,kmargin,(APPW-kmargin*7/2)/2,kmargin*4);
         }
        }];

    // 任务的处理 拒绝
    [self.taskDetailDic[@"opts"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        if ([obj isEqualToString:@"deal"])
        {
           UIButton *left=[AppUI createButtonWithText:self.leftBtnTitleStr andWithTextColor:WhiteColor andWithFontfloat:16 andWithBtnAction:^{

               UIAlertView *alert=[UIAlertView alertViewWithTitle:@"提示" message:@"你确定要拒绝当前任务吗？" cancelButtonTitle:@"取消" didDismissBlock:^(UIAlertView *alertView,NSInteger buttonIndex) {

                   if (buttonIndex==0)
                   {
                       // 拒绝任务
                       [API DealTaskWithTaskID:self.model.taskID ac:@"refuse" turnID:@"0" memo:@"" success:^(id res) {

                           if ([res[@"rt"] isEqual:@(1)])
                           {
                               [self.navigationController popViewControllerAnimated:NO];
                           }
                           else
                           {
                               [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
                           }

                       } failure:^(NSInteger stateCode) {

                       }];

                   }
               } otherButtonTitles:@"确定", nil];
               [alert show];
           }];
            [self.bottomView addSubview:left];
             left.backgroundColor=MainColor;
            ViewBorderRadius(left,kmargin/2,1,MainColor);
            left.frame=CGRectMake(kmargin*3/2,kmargin,(APPW-kmargin*7/2)/2,kmargin*4);
        }
         else if ([obj isEqualToString:@"acceptance"])
         {
             UIButton *left=[AppUI createButtonWithText:self.leftBtnTitleStr andWithTextColor:WhiteColor andWithFontfloat:16 andWithBtnAction:^{
                 UIAlertView *alert=[UIAlertView alertViewWithTitle:@"提示" message:@"你确定要拒绝验收当前任务吗？" cancelButtonTitle:@"取消" didDismissBlock:^(UIAlertView *alertView,NSInteger buttonIndex) {

                     if (buttonIndex==0)
                     {
                         // 拒绝验收
                         SelectProgressViewController *vc=[SelectProgressViewController new];
                         vc.taskID=self.model.taskID;
                         [self.navigationController pushViewController:vc animated:NO];
                     }
                 } otherButtonTitles:@"确定", nil];
                 [alert show];
             }];
             [self.bottomView addSubview:left];
             left.backgroundColor=MainColor;
             ViewBorderRadius(left,kmargin/2,1,MainColor);
             left.frame=CGRectMake(kmargin*3/2,kmargin,(APPW-kmargin*7/2)/2,kmargin*4);
         }
        }];

    // 任务的处理 拒绝
    [self.taskDetailDic[@"opts"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            if ([obj isEqualToString:@"deal"])
            {
                UIButton *right=[AppUI createButtonWithText:self.rightBtnTitleStr andWithTextColor:WhiteColor andWithFontfloat:16 andWithBtnAction:^{

                [API DealTaskWithTaskID:self.model.taskID ac:@"accept" turnID:@"" memo:@"" success:^(id res) {

                 if ([res[@"rt"] isEqual:@(1)])
                 {
                     dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                         [self.navigationController popViewControllerAnimated:NO];
                     });
                 }
                 else
                 {
                     [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
                 }

             } failure:^(NSInteger stateCode) {

             }];

            }];
            [self.bottomView addSubview:right];
            right.backgroundColor=MainColor;
            ViewBorderRadius(right,kmargin/2,1,MainColor);
            right.frame=CGRectMake(APPW-kmargin*3/2-(APPW-kmargin*7/2)/2,kmargin,(APPW-kmargin*7/2)/2,kmargin*4);
        }
        else if ([obj isEqualToString:@"acceptance"])
        {
            UIButton *right=[AppUI createButtonWithText:self.rightBtnTitleStr andWithTextColor:WhiteColor andWithFontfloat:16 andWithBtnAction:^{
                [API CheckTaskID:self.model.taskID ac:@"pass" turn_id:@"0" memo:@"" success:^(id res) {

                 if ([res[@"rt"] isEqual:@(1)])
                 {
                     dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                         // 返回到评价界面
                         EvaluateTaskViewController *vc=[EvaluateTaskViewController new];
                         vc.model=self.model;
                         [self.navigationController pushViewController:vc animated:NO];
                     });
                 }
                 else
                 {
                     [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
                 }

             } failure:^(NSInteger stateCode) {

             }];
            }];
            [self.bottomView addSubview:right];
            right.backgroundColor=MainColor;
            ViewBorderRadius(right,kmargin/2,1,MainColor);
            right.frame=CGRectMake(APPW-kmargin*3/2-(APPW-kmargin*7/2)/2,kmargin,(APPW-kmargin*7/2)/2,kmargin*4);
        }
    }];
}

-(UIView *)taskInfoView
{
    if (!_taskInfoView) {
        _taskInfoView=[[UIView alloc]initWithFrame:CGRectMake(0,0,APPW,0)];
        _taskInfoView.backgroundColor=WhiteColor;
        setViewHeight_Add(_taskInfoView,kmargin*2);

        UILabel *nameLabel=[self.taskInfoView getLabelWithText:self.model.creater_name font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
        [self.taskInfoView addSubview:nameLabel];
        nameLabel.frame=CGRectMake(kmargin*2,_taskInfoView.frame.size.height,60,30);
        setViewHeight_Add(_taskInfoView,kmargin*3.5);

        UILabel *timeLabel=[self.taskInfoView getLabelWithText:[NSString stringWithFormat:@"发布于 %@",self.model.create_time] font:Font_Number(16) textColor:LightGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
        [self.taskInfoView addSubview:timeLabel];
        timeLabel.frame=CGRectMake(CGRectGetMaxX(nameLabel.frame),kmargin*2,APPW-kmargin*9,kmargin*3);

        // 任务名称
        UILabel *taskNameLabel=[self.taskInfoView getLabelWithText:@"任务名称" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.taskInfoView addSubview:taskNameLabel];
        taskNameLabel.frame=CGRectMake(kmargin*2,_taskInfoView.frame.size.height,APPW/2,30);
        setViewHeight_Add(_taskInfoView,kmargin*3);

    if (![self.model.project_name isKindOfClass:[NSNull class]])
        {
            NSString *taskNameStr=self.model.project_name;
            CGFloat h=[Util GetHeightWithText:taskNameStr width:APPW-kmargin*4 limitHeight:MAXFLOAT font:16];
            UILabel *taskNameDetailLabel=[self.taskInfoView getLabelWithText:taskNameStr font:Font_Number(16) textColor:LightGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:0];
            [self.taskInfoView addSubview:taskNameDetailLabel];
            taskNameDetailLabel.frame=CGRectMake(kmargin*2,_taskInfoView.frame.size.height,APPW-kmargin*4,h);
            setViewHeight_Add(_taskInfoView,h+kmargin);
        }

        // 任务目标
        UILabel *taskGoalLabel=[self.taskInfoView getLabelWithText:@"任务目标" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
        [self.taskInfoView addSubview:taskGoalLabel];
        taskGoalLabel.frame=CGRectMake(kmargin*2,_taskInfoView.frame.size.height,APPW/2,30);
        setViewHeight_Add(_taskInfoView,kmargin*3);

        NSString *taskGoalStr=self.model.target_desc;
        CGFloat taskGoalH=[Util GetHeightWithText:taskGoalStr width:APPW-kmargin*4 limitHeight:MAXFLOAT font:16];
        UILabel *taskGoalDetailLabel=[self.taskInfoView getLabelWithText:taskGoalStr font:Font_Number(16) textColor:LightGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:0];
        [self.taskInfoView addSubview:taskGoalDetailLabel];
        taskGoalDetailLabel.frame=CGRectMake(kmargin*2,_taskInfoView.frame.size.height,APPW-kmargin*4,taskGoalH);
        setViewHeight_Add(_taskInfoView,taskGoalH+kmargin);

        // 执行方法
        UILabel *taskMethodLabel=[self.taskInfoView getLabelWithText:@"执行方法" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.taskInfoView addSubview:taskMethodLabel];
        taskMethodLabel.frame=CGRectMake(kmargin*2,_taskInfoView.frame.size.height,APPW/2,30);
        setViewHeight_Add(_taskInfoView,kmargin*3);

        NSString *taskMethodStr=self.model.exec_method;
        CGFloat taskMethodH=[Util GetHeightWithText:taskMethodStr width:APPW-kmargin*4 limitHeight:MAXFLOAT font:16];
        UILabel *taskMethodDetailLabel=[self.taskInfoView getLabelWithText:taskMethodStr font:Font_Number(16) textColor:LightGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:0];
        [self.taskInfoView addSubview:taskMethodDetailLabel];
        taskMethodDetailLabel.frame=CGRectMake(kmargin*2,_taskInfoView.frame.size.height,APPW-kmargin*4,taskMethodH);
        setViewHeight_Add(_taskInfoView,taskMethodH+kmargin*3/2);


        UIButton *fileBtn=[AppUI createButtonWithSupeView:self.taskInfoView andWithNormalImage:[UIImage imageNamed:@"addFile"] andWithSelectImage:[UIImage imageNamed:@"addFile"] andWithBtnAction:^{

            // 跳转到 附件列表 界面
            FileViewController *vc=[FileViewController new];
            vc.fileArr=self.model.attachments;
            [self.navigationController pushViewController:vc animated:NO];

        }];
        ViewBorderRadius(fileBtn,kmargin/2,.5,LightGreyColor);
        [self.taskInfoView addSubview:fileBtn];
        fileBtn.frame=CGRectMake(kmargin*2,_taskInfoView.frame.size.height,kmargin*4,kmargin*3);
        setViewHeight_Add(_taskInfoView,kmargin*5);


        UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0,_taskInfoView.frame.size.height,APPW,kmargin)];
        lineView.backgroundColor=BGColor;
        [self.taskInfoView addSubview:lineView];
        setViewHeight_Add(_taskInfoView,kmargin);
    }
    return _taskInfoView;
}

// tableView
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,APPW,(_isBottomView==YES)?(APPH):APPH) style:UITableViewStylePlain];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=MAIN_BACK_COLOR;
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
        self.tableView.tableFooterView=[UIView new];
        _tableView.tableHeaderView=self.taskInfoView;
        [self.tableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:CommonTableViewCellIdentifier];
        [self.tableView registerClass:[TaskRateTableViewCell class] forCellReuseIdentifier:TaskRateTableViewCellIdentifier];
        [self.tableView registerClass:[TaskLogTableViewCell class] forCellReuseIdentifier:TaskLogTableViewCellIdentifier];
    }
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0)
    {
        return 1;
    }
    else if (section==1)
    {
        return self.taskLogArr.count;
    }
    else
    {
        if ([self.model.status isEqualToString:@"10"])
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        CommonTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CommonTableViewCellIdentifier];
        [cell setCellInfoWithHeaderImage:@"" withTitle:@"查看流程" withSubTitle:@"" withArrow:YES];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.section==1)
    {
        TaskLogTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:TaskLogTableViewCellIdentifier];
        cell.model=self.taskLogArr[indexPath.row];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else
    {
        TaskRateTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:TaskRateTableViewCellIdentifier];
        [cell configCellWithRateDic:self.rateDic];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    if (indexPath.section==0)
    {
        // 跳转到 任务流程 界面
        TaskProcessViewController *vc=[TaskProcessViewController new];
        vc.taskID=self.model.taskID;
        vc.taskStatus=self.model.status;
        [self.navigationController pushViewController:vc animated:NO];
    }
    else if (indexPath.section==1)
    {
        TaskLogDetailViewController *vc=[TaskLogDetailViewController new];
        vc.model=self.taskLogArr[indexPath.row];
        [self.navigationController pushViewController:vc animated:NO];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return kmargin*5;
    }
    else if (indexPath.section==2)
    {
        return kmargin*18;
    }
    else
    {
        return kmargin*11;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==1)
    {
        UIView *headView=[UIView new];
        headView.backgroundColor=WhiteColor;

        UIView *lineView=[UIView new];
        lineView.frame=CGRectMake(0,0,APPW,1);
        [headView addSubview:lineView];
        lineView.backgroundColor=MAIN_BACK_COLOR;

        UILabel *logLabel=[headView getLabelWithText:@"任务日志" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
        logLabel.frame=CGRectMake(kmargin*2,kmargin,APPW/2,kmargin*3);
        [headView addSubview:logLabel];
        return headView;
    }
    else
    {
        return nil;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==1)
    {
        return kmargin*5;
    }
    else
    {
        return 0;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView=[UIView new];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==0)
    {
        return 0.0;
    }
    else
    {
        return kmargin*2;
    }
}

// 间隙置边
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end
